<?php
/**
 * 卡券的快照
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;
use Model\CouponSnapshotMod;
use Model\StoreMod;
use Pub\cache\Cache;

class CouponSnapshot extends Action
{
    private $CouponSnapshot = null;  //卡券快照
    private $Store = null;

	public function __construct()
	{
		parent::__construct();
        $this->CouponSnapshot = new CouponSnapshotMod();
	}

    /**
     * 获取卡券列表
     * @param uid 用户id
     * @param activity_id 活动id
     * @param type 卡券类型
     * @param limit 请求下标
     * @param store 是否请求门店的logo和名称
     */
    public function getCouponList()
    {
        $_limit = isset($this->_gpc['limit']) ? $this->_gpc['limit'] : array();
        $_where = array('uid'=>$this->_gpc['uid']);
        if($this->_gpc['activity_id'] != 0){
            $_where['activity_id'] = $this->_gpc['activity_id'];
        }
        if($this->_gpc['type'] != 'qb'){
            $_where['type'] = $this->_gpc['type'];
        }
        $_coupon = $this->CouponSnapshot->findAll($_where,array('order'=>array('id','asc'),'limit'=>$_limit),array('id','past_time','number','use_number','store_id','detail','type'));
        if(count($_coupon) > 0){
            foreach($_coupon as $_k=>$_v){
                $_coupon[$_k]['expires'] = false;
                if($_v['past_time'] == 1){
                    $_coupon[$_k]['time'] = '永久有效';
                }else{
                    $_coupon[$_k]['time'] = '有效期至'.date('Y-m-d',$_v['past_time']);
                    $_coupon[$_k]['expires'] = TIMESTAMP > ($_v['past_time']+86400) ? true : false;
                }
                $_detail = json_decode($_v['detail'],true);
                $_coupon[$_k]['coupon_type'] = $_v['type'];
                if(isset($_detail['cost_type'])) $_coupon[$_k]['type'] = $_detail['cost_type'];
                if(isset($_detail['cost'])) $_coupon[$_k]['cost'] = $_detail['cost'];
                $_coupon[$_k]['name'] = $_detail['name'];
                if(isset($_detail['icon'])) $_coupon[$_k]['cover'] = tomedia($_detail['icon']);
                if(isset($_detail['cover'])) $_coupon[$_k]['cover'] = tomedia($_detail['cover']);
                if(isset($this->_gpc['store']) && $_v['store_id'] != 0){
                    $_store = $this->storeIdToLogoName($_v['store_id']);
                    $_coupon[$_k]['store_name'] = $_store['name'];
                    $_coupon[$_k]['logo'] = $_store['logo'];
                }
            }
            $this->_result = array('status'=>'success','data'=>$_coupon);
        }
        $this->result($this->_result);
    }

    /**
     * 通过门店id获取门店的logo和名称
     * @param $_store_id
     * @return array 门店的信息
     */
    private function storeIdToLogoName($_store_id)
    {
        if(!$this->Store instanceof StoreMod) $this->Store = new StoreMod();
        $_store = $this->Store->findOne(array('id'=>$_store_id),array('name','logo'));
        $_store['logo'] = tomedia($_store['logo']);
        return $_store;
    }


    /**
     *通过快照ID返回卡券的详情和门店的详情
     * @param member_treasure_id  抢到的宝藏记录id
     */
    public function findOne()
    {
        $_data = $this->CouponSnapshot->findOne(array('id'=>$this->_gpc['snapshot_id']),array('store_id','detail','past_time','number','use_number','activity_id','type'));
        if($_data){
            if($_data['past_time'] == 1){
                $_data['expires'] = false;
                $_data['time'] = '永久有效';
            }else{
                $_data['time'] = '有效期至'.date('Y-m-d',$_data['past_time']);
                $_data['expires'] = TIMESTAMP > ($_data['past_time']+86400) ? true : false;
            }
            $_data['coupon_type'] = $_data['type'];
            $_detail = json_decode($_data['detail'],true);
            $_data['name'] = $_detail['name'];
            $_data['type'] = $_detail['cost_type'];
            $_data['cost'] = $_detail['cost'];
            $_data['rule'] = $_detail['rule'];
            if(isset($_detail['icon'])) $_data['icon'] = tomedia($_detail['icon']);
            if(isset($_detail['images'])) $_data['images'] = $_detail['images'];
            $this->Store = new StoreMod();
            $_store = $this->Store->findOne(array('id'=>$_data['store_id']),array('name','logo','address','location'));
            $_data['store_name'] = $_store['name'];
            $_data['logo'] = tomedia($_store['logo']);
            $_data['address'] = $_store['address'];
            $_data['location'] = $_store['location'] != '' ? json_decode($_store['location'],true) : false;
            unset($_data['detail'],$_data['past_time']);
            $this->_result = array('status'=>'success','data'=>$_data);
        }
        $this->result($this->_result);
    }


    /**
     * 获取卡券的核销二维码
     * @param member_treasure_id  抢到的宝藏记录id
     * @param uid 用户id
     */
    public function getCouponAfterQrcode()
    {
        $_after_key = array(
            'key'=> sha1($this->_gpc['snapshot_id'].$this->_gpc['uid'].TIMESTAMP),
            'expire'=>TIMESTAMP+30
        );
        Cache::set(Cache::USER_COUPON_AFTER_KEY,$_after_key,array('uid'=>$this->_gpc['uid']));
        $_url = murl('entry//iscan',array('m'=>'kuyuan_vbz','type'=>'couponAfter','snapshot_id'=>$this->_gpc['snapshot_id'],'uid'=>$this->_gpc['uid'],'scan_key'=>$_after_key['key'],'check_time'=>TIMESTAMP),true,true);
        $_qrcode_url = $this->getQrcode('user_coupon_after_qrcode',$this->_gpc['uid'],$_url,true);
        $this->_result = array('status'=>'success','data'=>array('src'=>$_qrcode_url,'expire'=>$_after_key['expire']));
        $this->result($this->_result);
    }

    /**
     * 获取一条简单数据
     * @param snapshot_id 快照id
     * @param return 返回的数据
     */
    public function easyFind()
    {
        $_return = array();
        if(isset($this->_gpc['return'])){
            $_return = $this->_gpc['return'];
        }
        $_one = $this->CouponSnapshot->findOne(array('id'=>$this->_gpc['snapshot_id']),$_return);
        if($_one){
            $this->_result = array('status'=>'success','data'=>$_one);
        }
        $this->result($this->_result);
    }


}